import {tabAdapter} from './adapter'
import {tabsConfig} from './data'
const lib = ao2.lib

export function mkTab(config) {
  config = tabsConfig  // 模拟数据

  // 用户配置
  const customSet = {
    select: (config.select || 0),
    tap: config.tap
  }

  const [menusData, contentsData] = tabAdapter(config.data, customSet.select);

  const contentList = ui_list({
    $$id: 'tabs-content',
    listClass: "tabsBoxes",
    itemClass: 'tabs-item-content',
    data: contentsData
  });

  const menuList = ui_list({
    $$id: 'tab-switch',
    listClass: "tabsMenus",
    itemClass: 'tabs-item-link',
    data: menusData,
    
    created(){
      this.currentIndex = customSet.select
    },

    itemMethod: {
      tap(e, param, inst) {
        const attr = inst.attr();
        let res = true
        if (lib.isFunction(customSet.tap)) {
          res = customSet.tap.call(this, e, param, inst)
        }
        if (res || typeof res === 'undefined') {
          this.switch(attr.index)
        }
      }
    },

    switch(index){
      let idx = parseInt(index)
      if (idx > -1) {
        this.currentIndex = idx
        menuList.select(index);
        contentList.select(index);
      }
    }
  });

  return (
    <View className="tabs-container line column box-disN">
      <menuList.UI />
      <contentList.UI />
    </View>
  );
}